import 'package:casdon/app/config/env/common_config.dart';
import 'package:casdon/core/network/i_api_client.dart';
import 'package:casdon/modules/login/data/api/login_api.dart';
import 'package:casdon/modules/login/data/api/request_emailCode_api.dart';
import 'package:casdon/modules/login/data/api/request_sms_api.dart';
import 'package:casdon/modules/login/data/api/validate_otp_api.dart';
import 'package:casdon/modules/login/data/repository/auth_repository.dart';

class AuthRepositoryImpl implements AuthRepository {
  final IApiClient _apiClient;

  AuthRepositoryImpl(this._apiClient);

  @override
  Future<ResponseSmsApi> sendSmsCode(RequestSmsApi request) async {
    final res = await _apiClient.postTyped<ResponseSmsApi>(
      '/auth-client/v2/sendSmsVerifyCode',
      data: request.toJson(),
      fromJsonT: (json) =>
          ResponseSmsApi.fromJson(json as Map<String, dynamic>),
    );
    return res?.data ?? ResponseSmsApi(success: false);
  }

  @override
  Future<ResponseValidateOtpApi> vierfyCode(
    RequestValidateOtpApi request,
  ) async {
    final res = await _apiClient.postTyped<ResponseValidateOtpApi>(
      '/auth-client/validateCode',
      data: request.toJson(),
      fromJsonT: (json) => json is bool
          ? ResponseValidateOtpApi(result: json)
          : ResponseValidateOtpApi(result: false),
    );
    return res?.data ?? ResponseValidateOtpApi(result: false);
  }

  @override
  Future<bool> sendEmailCode(RequestEmailcodeApi request) async {
    await _apiClient.postTyped<dynamic>(
      '/auth-client/v2/sendEmailVerificationCode',
      data: request.toJson(),
      fromJsonT: (json) =>
          json, // 这里会返回null，因为接口无论成功与失败，以及是否有业务错误，都是data = null
    );
    // 如果能执行到这里，说明 code == 200，返回 true
    return true;
  }

  @override
  Future<ResponseLogin> login(RequestLogin request) async {
    final res = await _apiClient.postTyped<ResponseLogin>(
      "/auth-client/login",
      data: request.toJson(),
      fromJsonT: (json) => ResponseLogin.fromJson(json as Map<String, dynamic>),
      encrypt: true,
    );
    return res?.data ?? ResponseLogin(access_token: "", client_id: "");
  }

  @override
  Future<void> postUpdatePwd(String userName, String pwd) async {
    final res = await _apiClient.post<void>(
      '/auth-client/updatePwd',
      encrypt: true,
      data: {'userName': userName, 'newPassword': pwd},
    );
    return;
  }

  @override
  Future<void> postDelAccount() async {
    final _ = await _apiClient.post<void>('/auth-client/v2/cancel');
    return;
  }

  @override
  Future<bool> postCheckIsAlreadyRegister(String userName) async {
    final res = await _apiClient.postTyped<bool>(
      '/auth-client/checkIsAlreadyRegister',
      data: {"userName": userName},
      fromJsonT: (json) => json is bool ? json : false,
    );
    return res?.data ?? false;
  }

  @override
  Future<void> postRegister(
    String userName,
    String grantType,
    String password,
  ) async {
    final _ = await _apiClient.post<void>(
      '/auth-client/register',
      data: {
        "userName": userName,
        "grantType": grantType,
        "clientId": CommonConfig.app_clientID,
        "password": password,
        "userType": "app_user",
      },
      encrypt: true,
    );
    return;
  }

  @override
  Future<void> postLogOut() async {
    final _ = await _apiClient.post<void>('/auth-client/logout');
    return;
  }
}
